package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.transport.AgreedCarOfferEntity;
import com.sgb.kjwl.view.ui.activity.transport.OperateEnquiryDetail;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @Author : 熊丽
 * @date :2021/4/9 0009$ 14:34$
 * @Description ://$
 */
public class InquiryDetailModel extends AndroidViewModel {
    public MutableLiveData<OperateEnquiryDetail> OperateEnquiryDetail = new MutableLiveData<>();
    public MutableLiveData<AgreedCarOfferEntity> AgreedCarOfferEntityData = new MutableLiveData<>();

    public InquiryDetailModel(@NonNull Application application) {
        super(application);
    }

    public void getOperateMyInquiryDetails(Context context,String no,boolean isGoodsOwner) {
        Map<String, Object> map = new HashMap<>();
        map.put("carOfferNo", no);
        map.put("isGoodsOwner",isGoodsOwner);
        DialogHelper.showProgressDialog(context, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getLogisticsAPI().getoperateMyInquiryDetails(no,isGoodsOwner).enqueue(new Callback<BaseEntity<OperateEnquiryDetail>>() {
            @Override
            public void onResponse(Call<BaseEntity<OperateEnquiryDetail>> call, Response<BaseEntity<OperateEnquiryDetail>> response) {
                DialogHelper.dismissProgressDialog();

                if (response.body() == null || response.body().getState() == null || response.body().getData() == null)
                    return;

                if (response.body().getState().equals("ok")) {
                    OperateEnquiryDetail.postValue(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<OperateEnquiryDetail>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    public void GetagreedCarOffer(Context context,String no,String carNo,OperateEnquiryDetail operateEnquiryDetail) {
        Map<String, Object> map = new HashMap<>();
        map.put("goodsSourceNo", no);
        map.put("carNo",carNo);
        DialogHelper.showProgressDialog(context, "数据加载中...");
        NewAPIManager.getInstance().getLogisticsAPI().checkCarStatues(map).enqueue(new Callback<BaseEntity<AgreedCarOfferEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<AgreedCarOfferEntity>> call, Response<BaseEntity<AgreedCarOfferEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if ("ok".equals(response.body().getState())) {
                    AgreedCarOfferEntityData.postValue(response.body().getData());
                }else if ("400".equals(response.body().getCode())||"401".equals(response.body().getCode())){
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<AgreedCarOfferEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(context, t.getMessage());
            }
        });
    }
}
